package com.bdqn.t308.web;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class String2DateConverter implements Converter<String,Date> {

    private String datePattern;

    /**
     * 有参构造函数
     * @param datePattern
     */
    public String2DateConverter(String datePattern){
        this.datePattern = datePattern;
    }

    @Override
    public Date convert(String s) {
        Date date = null;
        try {
            date = new SimpleDateFormat(datePattern).parse(s);  //将字符串转日期格式
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }
}
